<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="标注员" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入标注员"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div style="font-size: 12px; color: gray">{{ taskPath }}</div>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-tooltip effect="light" content="同步最新结果" placement="right-end">
          <el-button circle icon="el-icon-refresh" size="mini" @click="handleRefresh" />
        </el-tooltip>
      </el-col>
      <el-col :span="1.5">
        <el-tooltip effect="light" content="导出" placement="right-end">
          <el-button circle icon="el-icon-download" size="mini" @click="handleExport" />
        </el-tooltip>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="markFactoryReportList">
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="标注工厂名称" align="center" prop="deptName" />
      <el-table-column label="标注员" align="center" prop="userName" />
      <el-table-column v-if="rectTotalCount > 0" label="二维矩形框" align="center" prop="rectCount" />
      <el-table-column v-if="boxTotalCount > 0" label="二维立体框" align="center" prop="boxCount" />
      <el-table-column v-if="lineTotalCount > 0" label="二维折线" align="center" prop="lineCount" />
      <el-table-column v-if="polyTotalCount > 0" label="二维分割" align="center" prop="polyCount" />
      <el-table-column v-if="point1TotalCount > 0" label="二维单点" align="center" prop="point1Count" />
      <el-table-column v-if="rectPoint1TotalCount > 0" label="二维框带点" align="center" prop="rectPoint1Count" />
      <el-table-column v-if="cubeTotalCount > 0" label="三维立体框" align="center" prop="cubeCount" />
      <el-table-column v-if="splitTotalCount > 0" label="三维分割" align="center" prop="splitCount" />
      <el-table-column v-if="otherTotalCount > 0" label="其他类型" align="center" prop="otherCount" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getQaTask } from '@/api/mark/qaTask'
import { listMarkFactoryReport, exportMarkFactoryFeport, refreshFactoryReport } from '@/api/mark/markFactoryReport'

export default {
  name: 'MarkFactoryReport',
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // mark_factory_report表格数据
      markFactoryReportList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskId: 118,
        taskName: undefined,
        deptId: undefined,
        deptName: undefined,
        userId: undefined,
        userName: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      rectTotalCount: 0,
      boxTotalCount: 0,
      lineTotalCount: 0,
      polyTotalCount: 0,
      point1TotalCount: 0,
      rectPoint1TotalCount: 0,
      cubeTotalCount: 0,
      splitTotalCount: 0,
      otherTotalCount: 0,
      taskPath: '',
    }
  },
  created() {
    if (this.$route.query.taskId) {
      this.queryParams = {
        taskId: this.$route.query.taskId,
      }
      this.getList()
    }
  },
  methods: {
    /** 标注工厂统计报表 */
    getList() {
      this.taskPath = ''
      if (this.$route.query.taskId && this.$route.query.taskId !== 0) {
        getQaTask(this.$route.query.taskId).then((response) => {
          if (response.data) {
            this.taskPath = response.data.taskName
            this.markType = response.data.markType
            if (response.data.parentId !== 0) {
              getQaTask(response.data.parentId).then((response) => {
                this.taskPath = response.data.taskName + '@' + this.taskPath
                if (response.data.parentId !== 0) {
                  getQaTask(response.data.parentId).then((response) => {
                    this.taskPath = response.data.taskName + '@' + this.taskPath
                  })
                }
              })
            }
          }
        })
      }
      this.loading = true
      listMarkFactoryReport(this.queryParams).then((response) => {
        this.rectTotalCount = 0
        this.boxTotalCount = 0
        this.lineTotalCount = 0
        this.polyTotalCount = 0
        this.point1TotalCount = 0
        this.rectPoint1TotalCount = 0
        this.cubeTotalCount = 0
        this.splitTotalCount = 0
        this.otherTotalCount = 0
        this.markFactoryReportList = response.rows
        this.markFactoryReportList.forEach((row) => {
          this.rectTotalCount += row.rectCount
          this.boxTotalCount += row.boxCount
          this.lineTotalCount += row.lineCount
          this.polyTotalCount += row.polyCount
          this.point1TotalCount += row.point1Count
          this.rectPoint1TotalCount += row.rectPoint1Count
          this.cubeTotalCount += row.cubeCount
          this.splitTotalCount += row.splitCount
          this.otherTotalCount += row.otherCount
        })
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        reportId: undefined,
        taskId: undefined,
        taskName: undefined,
        deptId: undefined,
        deptName: undefined,
        userId: undefined,
        userName: undefined,
        rectCount: undefined,
        boxCount: undefined,
        lineCount: undefined,
        polyCount: undefined,
        point1Count: undefined,
        rectPoint1Count: undefined,
        cubeCount: undefined,
        splitCount: undefined,
        otherCount: undefined,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有标注工厂统计报表数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return exportMarkFactoryFeport(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
        })
        .catch(function () {})
    },
    handleRefresh() {
      refreshFactoryReport(this.queryParams).then((response) => {
        if (response.code == 200) {
          this.msgSuccess(response.msg)
        } else {
          this.msgError(response.msg)
        }
      })
    },
  },
}
</script>
